package kr.saeildamil.androidStudy_second.gesture;

import kr.saeildamil.androidStudy_second.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.GestureDetector.OnGestureListener;
import android.widget.TextView;

public class GestureNavi extends Activity {
	TextView mResult;
	TextView mTextCount;

	int mCount = 10;
	GestureDetector mDetector;
	final static int DISTANCE = 200;
	final static int VELOCITY = 300;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch31_gesturenavi);

		mResult = (TextView) findViewById(R.id.result);
		mTextCount = (TextView) findViewById(R.id.count);

		mDetector = new GestureDetector(this, mGestureListener);
		mDetector.setIsLongpressEnabled(false);
		mTextCount.setText("" + mCount);
	}
	public boolean onTouchEvent(MotionEvent event) {
		return mDetector.onTouchEvent(event);
	};

	OnGestureListener mGestureListener = new OnGestureListener() {

		public boolean onSingleTapUp(MotionEvent e) {return false;}
		public void onShowPress(MotionEvent e) {}
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {return false;}
		public void onLongPress(MotionEvent e) {}
		
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			if(Math.abs(velocityX) > VELOCITY){ //좌우로 fling했을때 기준 가속도
				if(e1.getX() - e2.getX() > DISTANCE || e1.getY() - e2.getY() > DISTANCE){
					//<----- 우에서 좌로 이동
					mCount--;
				}
				if(e2.getX() - e1.getX() > DISTANCE || e2.getY() - e1.getY() > DISTANCE){
					//-----> 좌에서 우로 이동
					mCount++;
				}
				mTextCount.setText(Integer.toString(mCount));
				String text = String.format(
						"* x축 거리 = %d, y축 거리 = %d, \n* x축 속도= %d, y축 속도= %d", 
						(int)Math.abs(e1.getX()-e2.getX()),(int)Math.abs(e1.getY()-e2.getY()), 
						(int)velocityX, (int)velocityY);
				mResult.setText(text);
				
			}
			return true;
		}
		public boolean onDown(MotionEvent e) {return false;}
	};
}
